package com.shelpe.services.lexicon.entity;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.shelpe.services.lexicon.constants.Status;
import com.shelpe.services.lexicon.entity.metrics.InsightMetrics;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Document(collection="adgroup_records")
public class AdgroupRecord extends BaseEntity{

	@Id
	private String recordID;
	
	@Indexed
	private long tanentID;
	private String nickName;
	@Indexed
	private long campaignID;
	private long itemID;
	private int[] categoryIDs;
	@Indexed
	private long adgroupID;
	
	private Action action = Action.create;//操作方式，create - 为adgroup第一次创建的record
	private Status status = Status.init;//状态，init - 初始化， sync - 同步过
	private SyncType syncType; //同步类型。 active - 主动，指的是调用同步接口。 passive - 被动，指的是查找推荐词时被动触发
	private Date calcTime;
	private Date pqrTime;//计算pqr和similarity时间
	private Date tagTime;//计算标签的时间
	
	@Indexed
	private Date postTime;
	
	private KeywordSource source;
	
	private int click;
	private int recommendCount;
	
	private boolean merged;
	
	public void addKeywordMetric(KeywordMetric km){
		if(km != null && km.getMetric() != null){
			if(click == 0){
				this.click = km.getMetric().getClick();
			}else{
				this.click = (this.click + km.getMetric().getClick()) /2;
			}
		}
	}
	
	public void addKeywordMetric(InsightMetrics metric){
		if(metric != null){
			if(click == 0){
				this.click = metric.getClick();
			}else{
				this.click = (this.click + metric.getClick()) /2;
			}
		}
	}

	public int getRootCategoryID(){
		int id = 0;
		if(this.categoryIDs != null && this.categoryIDs.length > 0){
			id = this.categoryIDs[0];
		}
		return id;
	}
	
	public static enum Action{
		create,update
	}
	public static enum SyncType{
		active, passive
	}
	
}
